package app.bigdata;

import java.util.ArrayList;

public class GetAVG{
	
	private static double avgLat;
	private static double avgLong;
	
	public GetAVG(ArrayList<StarData> arrayList){
		int n = arrayList.size();
		double[] x = new double[n];
		double[] y = new double[n];
		double[] z = new double[n];
		double[] lat = new double[n];
		double[] lon = new double[n];
		double sumX = 0;
		double sumY = 0;
		double sumZ = 0;
		double bSum = 0;
		
		for(int i = 0 ; i < n ; i++){
			lat[i] = deg2rad(arrayList.get(i).getLatitude());
			lon[i] = deg2rad(arrayList.get(i).getLongitude());
			
			x[i] = Math.cos(lat[i])*Math.cos(lon[i]);
			y[i] = Math.cos(lat[i])*Math.sin(lon[i]);
			z[i] = Math.sin(lat[i]);
			
			sumX = sumX + x[i]*arrayList.get(i).getBrightness();
			sumY = sumY + y[i]*arrayList.get(i).getBrightness();
			sumZ = sumZ + z[i]*arrayList.get(i).getBrightness();
			
			bSum = bSum + arrayList.get(i).getBrightness();
		}
		double avgX = sumX/bSum;
		double avgY = sumY/bSum;
		double avgZ = sumZ/bSum;
		double r = Math.sqrt(avgX*avgX + avgY*avgY);
		
		avgLong = Math.atan2(avgY,avgX);
		avgLat = Math.atan2(avgZ,r);
		
	}
	
	/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	/*::  This function converts decimal degrees to radians             :*/
	/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	private static double deg2rad(double deg) {
		return (deg * Math.PI / 180.0);
	}

	/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	/*::  This function converts radians to decimal degrees             :*/
	/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	private static double rad2deg(double rad) {
		return (rad * 180.0 / Math.PI);
	}
	
	public static double getAVGLatitude(){
		return rad2deg(avgLat);
	}
	
	public static double getAVGLongitude(){
		return rad2deg(avgLong);
	}
}